package com.example.star.mapper;

import com.example.star.entity.Favorite;
import com.example.star.entity.Perimeter;
import com.example.star.entity.ShoppingCart;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface FavoriteMapper {
    @Insert("INSERT INTO favorites (username,imageResource, starName, title, productType, prices, freight, deliveryAddress) " +
            "VALUES (#{userName},#{imageResource}, #{starName}, #{title}, #{productType}, #{prices}, #{freight}, #{deliveryAddress})")
    void insertFavorite(Favorite favorite);

    @Select("SELECT * FROM favorites")
    List<Favorite> getAllFavorites();

    @Select("SELECT * FROM favorites WHERE username = #{userName}")
    List<Favorite> getFavoriteByuserName(String userName);

    @Delete("DELETE FROM favorites WHERE id = #{id}")
    void deleteFavoriteById(int id);

    @Select("SELECT * FROM favorites WHERE title = #{title}")
    Favorite getFavoriteByTitle(String title);
    @Select("SELECT * FROM favorites WHERE id = #{id}")
    Favorite getFavoriteById(int id);


    @Select("SELECT * FROM favorites LIMIT #{offset}, #{limit}")
    List<Favorite> getFavoritesByPage(@Param("offset") int offset, @Param("limit") int limit);
    @Select("SELECT COUNT(*) FROM favorites")
    int getTotalFavorites();

    @Select("SELECT COUNT(*) FROM favorites WHERE username = #{userName} AND title = #{title}")
    int checkFavorite(String userName, String title);

    @Delete("DELETE FROM favorites WHERE username = #{userName} AND title = #{title}")
    int deleteFavorite(String userName, String title);

    // 分页查询指定用户的收藏信息
    @Select("SELECT * FROM favorites WHERE username = #{userName} LIMIT #{offset}, #{limit}")
    List<Favorite> getFavoritesByUserNameAndPage(@Param("userName") String userName, @Param("offset") int offset, @Param("limit") int limit);

    // 获取指定用户的收藏总数
    @Select("SELECT COUNT(*) FROM favorites WHERE username = #{userName}")
    int getTotalFavoritesByUserName(@Param("userName") String userName);
}
